package com.lideru.backstage.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lideru.backstage.entity.Book;
import com.lideru.backstage.entity.Favorite;
import com.lideru.backstage.mapper.BookMapper;
import com.lideru.backstage.mapper.FavoriteMapper;
import com.lideru.backstage.utils.JWTUtils;
import com.lideru.backstage.utils.common.Result;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;


/**
 * @author lideru
 */
@RestController
@RequestMapping("/favorite")
public class FavoriteController {
    @Resource
    FavoriteMapper favoriteMapper;
    @Resource
    private BookMapper bookMapper;

    @PostMapping
    public Result<?> save(@RequestBody Favorite favorite, HttpServletRequest request) {
        Favorite b = favoriteMapper.selectOne(Wrappers.<Favorite>lambdaQuery().eq(Favorite::getBookId, favorite.getBookId()).
                eq(Favorite::getUserId, JWTUtils.getUserId(request.getHeader("token"))));
        if (b != null) {
            Book book = bookMapper.selectById(b.getBookId());
            if (book != null) {
                b.setName(book.getName());
                b.setCover(book.getCover());
                b.setGmtModified(new Date());
                b.setUserId(JWTUtils.getUserId(request.getHeader("token")) );
                favoriteMapper.updateById(b);
                return Result.success();
            } else {
                return Result.error("-1", "新增失败！");
            }

        } else {
            Book book = bookMapper.selectById(favorite.getBookId());
            if (book != null) {
                Favorite Favorite = new Favorite();
                Favorite.setCover(book.getCover());
                Favorite.setName(book.getName());
                Favorite.setBookId(book.getId());
                Favorite.setUserId(JWTUtils.getUserId(request.getHeader("token")));
                Favorite.setGmtCreate(new Date());
                Favorite.setGmtModified(new Date());
                int i = favoriteMapper.insert(Favorite);
                if (i < 1) {
                    return Result.error("-1", "新增失败！");
                }
                return Result.success();
            } else {
                return Result.error("-1", "新增失败！");
            }
        }
    }

    @PutMapping
    public Result<?> update(@RequestBody Favorite favorite) {
        favorite.setGmtModified(new Date());
        int i = favoriteMapper.updateById(favorite);
        if (i < 1) {
            return Result.error("-1", "修改失败！");
        }
        return Result.success();
    }

    @DeleteMapping("/{id}")
    public Result<?> delete(@PathVariable Integer id) {
        Favorite Favorite1 = favoriteMapper.selectOne(Wrappers.<Favorite>lambdaQuery()
                .eq(Favorite::getId, id));
        if (Favorite1 != null) {
            favoriteMapper.deleteById(id);
            return Result.success();
        }
        return Result.error("403", "你没有权限");

    }

    @GetMapping("getAllFavorite")
    public Result<?> getAllFavorite() {
        return Result.success(favoriteMapper.selectList(null));
    }

    @GetMapping("getUserAllFavorite")
    public Result<?> getUserAllFavorite(@RequestParam(defaultValue = "1") Integer pageNum,
                                              @RequestParam(defaultValue = "10") Integer pageSize,
                                              @RequestParam(defaultValue = "") String search, HttpServletRequest request) {
        return Result.success(
                favoriteMapper.selectPage(new Page<>(pageNum, pageSize), Wrappers.<Favorite>lambdaQuery().
                        eq(Favorite::getUserId, JWTUtils.getUserId(request.getHeader("token")))
                        .last("order by gmt_modified  desc")));
    }

    @GetMapping
    public Result<?> findPage(@RequestParam(defaultValue = "1") Integer pageNum,
                              @RequestParam(defaultValue = "10") Integer pageSize,
                              @RequestParam(defaultValue = "") String search, HttpServletRequest request) {
        return Result.success(
                favoriteMapper.selectPage(new Page<>(pageNum, pageSize), Wrappers.<Favorite>lambdaQuery().
                        eq(Favorite::getUserId, JWTUtils.getUserId(request.getHeader("token")))
                        .like(Favorite::getName, search)));
    }

    /**
     * 统计数据
     *
     * @return
     */
    @GetMapping("/count")
    public Result<?> count(HttpServletRequest request) {
//        User user = getUser(); // 当前登录的用户信息
        return Result.success(favoriteMapper.countFavorite( JWTUtils.getUserId(request.getHeader("token"))));
    }


}
